from django.contrib.auth.models import User, Group
from django.utils.timezone import now
from rest_framework import mixins
from rest_framework import serializers

#导入在models层写的数据模型UpItems，UpDatas，Notes，Notenum
from bookshelf.models import Book


class BookSerializer(serializers.ModelSerializer):
    """
    Book模型的序列化器
    """
    #序列化时间格式
    createdDate = serializers.DateTimeField(required=False, format='%Y-%m-%d %H:%M:%S')
    lastEditedDate = serializers.DateTimeField(required=False, format='%Y-%m-%d %H:%M:%S')
    class Meta:
        model = Book                            #声明模型
        fields = '__all__'                         #声明序列化字段，如果需要显示到前端的数据不多，也可以针对性的 fields = ['gamename', 'gamepic'] 来显示

    def get_book(self, obj):
        """
        在序列化器重写get请求显示
        """
        book = Book.objects.filter(deleted__exact='0')             #因为设置了假删除，这里的get请求获取到数据库中没有标识删除的数据
        book_serializers = BookSerializer(book, many=True)
        return book_serializers.data